package kr.ac.hanyang.cs.park;

import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class NewTask extends ListActivity {
	private ArrayList<TaskInfo> tasks					= new ArrayList<TaskInfo>();


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);

	    // TODO Auto-generated method stub
	    tasks											= CastSpell.getNewTaskList(this);

	    TaskListAdapter taskListAdapter					= new TaskListAdapter(this, R.layout.newtaskitems, tasks);
        setListAdapter(taskListAdapter);

    }

	public NewTask() {
	    
	}


    private class TaskListAdapter extends ArrayAdapter<TaskInfo> {
    	private ArrayList<TaskInfo> items;
    	
    	public TaskListAdapter(Context context, int textViewResourceId, ArrayList<TaskInfo> items) {
    		super(context, textViewResourceId, items);
    		this.items								= items;
    	}


    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		View v									= convertView;

    		if (v == null) {
    			LayoutInflater vi					= (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    			v									= vi.inflate(R.layout.newtaskitems, null);
            }


    		TaskInfo task							= items.get(position);

    		if (task != null) {
    			ImageView icon						= (ImageView)v.findViewById(R.id.task_imgIcon);
    			TextView name						= (TextView) v.findViewById(R.id.task_txtTaskName);
    			TextView desc						= (TextView) v.findViewById(R.id.task_txtDescription);

    			if (icon != null)
    				icon.setImageDrawable(task.getIcon());

    			if (name != null)
    				name.setText(task.getTaskName());

    			if (desc != null)
    				desc.setText(task.getDescription());

    			// 현재 item의 position을 버튼에 tagging한 후, 클릭 시 이벤트 리스터에 넘김
    			v.setTag(position);
    			v.setOnClickListener(itemClickListener);
            }

            return v;
    	}


    	private View.OnClickListener itemClickListener = new View.OnClickListener() {
    		public void onClick(View v) {
    			TaskInfo selectedTask				= items.get((Integer) v.getTag());
    			Intent intent						= null;

    			switch (selectedTask.getTaskType()) {
    				case LAUNCH_APP:
    				case CALL_ADDR:
    				case SEND_SMS:
    					intent						= new Intent(NewTask.this, SelectOption.class);
    					intent.putExtra("TYPE", selectedTask.getTaskType().toString());
    					startActivity(intent);
    					break;
    				
//    				case FLASH_ON:
//    				case FLASH_OFF:
    				case WIFI_ON:
    				case WIFI_OFF:
    				case BT_ON:
    				case BT_OFF:
    	    			ParcelableTask taskToSend	= new ParcelableTask(selectedTask);
    					intent						= new Intent(NewTask.this, RegisterTask.class);
    					intent.putExtra("TASK", taskToSend);
    					startActivity(intent);
    					break;

    				// LAUNCH TASK HERE
    				default:
    					Log.d("INFO", "NEED implement on NewTask.class :: "+ selectedTask.getTaskType().toString());
    			}

    			finish();
    		}
    		
    	};

    }
}
